﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace CommModule.Messages
{
    public class GenericMessage : Convertible
    {
        private String _objectType;
        private String _objectString;

        private string _signature;

        private int _number;

        public GenericMessage(String objectType, String objectString)
        {
            _objectType = objectType;
            _objectString = objectString;
        }

        public GenericMessage()
        {
        }

        public String ObjectType
        {
            get { return _objectType; }
            set { _objectType = value; }
        }

        public String ObjectString
        {
            get { return _objectString; }
            set { _objectString = value; }
        }

        public String Signature
        {
            get { return _signature; }
            set { _signature = value; }
        }

        public int Number
        {
            get { return _number; }
            set { _number = value; }
        }
        
        public void ConvertToCanonic(XmlWriter xmlWriter)
        {
            xmlWriter.WriteStartElement("GenericMessage");

            if (_objectType != null)
            {
                xmlWriter.WriteStartElement("ObjectType");
                xmlWriter.WriteString(_objectType);
                xmlWriter.WriteEndElement();
            }

            if (_objectString != null)
            {
                xmlWriter.WriteStartElement("ObjectString");
                xmlWriter.WriteString(_objectString);
                xmlWriter.WriteEndElement();
            }

            if (_signature != null)
            {
                xmlWriter.WriteStartElement("Signature");
                xmlWriter.WriteString(_signature);
                xmlWriter.WriteEndElement();
            }

            xmlWriter.WriteStartElement("Number");
            xmlWriter.WriteString(_number.ToString());
            xmlWriter.WriteEndElement();            

            xmlWriter.WriteEndElement();
        }

        public void ConvertFromCanonic(XmlTextReader xmlTextReader)
        {
            xmlTextReader.ReadStartElement();

            if (xmlTextReader.Name == "ObjectType")
            {
                _objectType = xmlTextReader.ReadElementContentAsString();
            }

            if (xmlTextReader.Name == "ObjectString")
            {
                _objectString = xmlTextReader.ReadElementContentAsString();
            }

            if (xmlTextReader.Name == "Signature")
            {
                _signature = xmlTextReader.ReadElementContentAsString();
            }

            if (xmlTextReader.Name == "Number")
            {
                _number = xmlTextReader.ReadElementContentAsInt();
            }

            xmlTextReader.ReadEndElement();
        }
    }
}
